const mongoose = require('mongoose')

const StudentSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 2,
    maxlength: 10
  },
  age: {
    type: Number,
    min: 0,
    max: 100,
    default: 0
  },
  sex: {
    type: String,
    enum: ['0', '1', '2'], // 0: 未知、1：男、2：女
    default: '0'
  },
  email: {
    type: String,
    validate: {
      // validator: () => {
      // }
      validator(v) {
        return /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(v)
      },
      message: 'email verify faild.'
    }
  },
  hobbies: [String],
  collage: String,
  enterDate: {
    type: Date,
    default: Date.now
  }
})

const Student = mongoose.model('Student', StudentSchema, 'Student')

module.exports = { Student }
